//
//  MYMySetMsgWarnVC.m
//  MuYing
//
//  Created by tbb on 2019/8/8.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYMySetMsgWarnVC.h"
#import "MYMySetMsgWarnModel.h"
@interface MYMySetMsgWarnVC ()
@property (nonatomic, strong) MYMySetMsgWarnModel * sateModel;

@property (nonatomic, strong) NSMutableDictionary * updateParameters;
@end

@implementation MYMySetMsgWarnVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    self.navigationItem.title = kYQLS(@"消息提醒");
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = kAPPTABLEVIEW_BG_COLOR;
    kYQREGISTERTABLEVIEWCNIBHeaderFooter(@"MYMySetMsgWarnTitleheader")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMySetTextCell");
    kYQREGISTERTABLEVIEWCELL([YQAddressAddSwitchCell class])
    [self getNoitSateData];
}

-(void)getNoitSateData {
    
    YQSHOWLOADINGTIPS(@"")
    [YQPNetWorkingManager sendPostRequest:kPOST_USER_QUERY_SETTING_URL parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            self.sateModel = [MYMySetMsgWarnModel modelWithJSON:responseObject.data];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error)
    }];
    
}

//修改状态
-(void)postUpdateSateData {
    YQSHOWLOADINGTIPS(@"")
    ws(self)
    [YQPNetWorkingManager sendPostRequest:kPOST_USER_MODIFICATION_SETTING_URL parameters:self.updateParameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            [weakself getNoitSateData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error)
    }];
}

-(void)topSwitchAction:(UISwitch *)sender {
    NSInteger row = sender.tag - 11100;
    if (row == 0) {
        self.updateParameters[@"attentionRemind"] = self.sateModel.attentionRemind ? @"0":@"1";
    }else if (row == 1) {
        self.updateParameters[@"commentsRemind"] = self.sateModel.commentsRemind ? @"0":@"1";
    }else if (row == 2){
        self.updateParameters[@"orderRemind"] = self.sateModel.orderRemind ? @"0":@"1";
    }else{
        self.updateParameters[@"integralRemind"] = self.sateModel.integralRemind ? @"0":@"1";
    }
    [self postUpdateSateData];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    if (self.sateModel.systemRemind) {
        return 4;
    }
    return 0;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            MYMySetTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetTextCell"];
            textCell.titleLb.text = kYQLS(@"系统消息开关");
            textCell.subLb.text = self.sateModel.systemRemind ? kYQLS(@"已开启") : kYQLS(@"已关闭");
            return textCell;
        }else{
            YQAddressAddSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQAddressAddSwitchCell"];
            cell.textField.textInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            cell.isTextFieldEdit = false;
            cell.textField.text = kYQLS(@"关注更新");
            cell.topSwitch.on = self.sateModel.attentionRemind;
            
            return cell;
        }
    }else{
        YQAddressAddSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQAddressAddSwitchCell"];
        cell.textField.textInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        cell.isTextFieldEdit = false;
        
        if (indexPath.row == 0) {
            cell.textField.text = kYQLS(@"关注更新");
            cell.topSwitch.on = self.sateModel.attentionRemind;
        }else if (indexPath.row == 1) {
            cell.textField.text = kYQLS(@"新评论、关注");
            cell.topSwitch.on = self.sateModel.commentsRemind;
        }else if (indexPath.row == 2) {
            cell.textField.text = kYQLS(@"订单变动通知");
            cell.topSwitch.on = self.sateModel.orderRemind;
        }else{
            cell.textField.text = kYQLS(@"积分变动通知");
            cell.topSwitch.on = self.sateModel.integralRemind;
        }
        cell.topSwitch.tag = 11100 + indexPath.row;
        [cell.topSwitch addTarget:self action:@selector(topSwitchAction:) forControlEvents:(UIControlEventValueChanged)];
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            //取消订单
             QMUIAlertController *alert = [[QMUIAlertController alloc] initWithTitle:kYQLS(@"确定打开") message:@"" preferredStyle:(QMUIAlertControllerStyleAlert)];
             
             [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"我再想一想") style:(QMUIAlertActionStyleCancel) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
                 
             }]];
             alert.alertCancelButtonAttributes = @{NSForegroundColorAttributeName:kAPPThemeColor,NSFontAttributeName:UIFontBoldMake(17),NSKernAttributeName:@(0)};
             
             [alert addAction:[QMUIAlertAction actionWithTitle:kYQLS(@"确定") style:(QMUIAlertActionStyleDefault) handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
                 self.updateParameters[@"systemRemind"] = self.sateModel.systemRemind ? @"0":@"1";
                 [self postUpdateSateData];
             }]];
             alert.alertButtonAttributes = @{NSForegroundColorAttributeName:HexColor(0xB7B7B7),NSFontAttributeName:kSystemMedium(17),NSKernAttributeName:@(0)};
             
             [alert showWithAnimated:YES];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        MYMySetMsgWarnTitleheader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYMySetMsgWarnTitleheader"];
        return header;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 47;
    }
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

-(NSMutableDictionary *)updateParameters {
    if (!_updateParameters) {
        _updateParameters = [NSMutableDictionary dictionary];
        _updateParameters[@"attentionRemind"] = self.sateModel.attentionRemind ? @"1":@"0";
        _updateParameters[@"autoplayVideo"] = self.sateModel.autoplayVideo ? @"1":@"0";
        _updateParameters[@"commentsRemind"] = self.sateModel.commentsRemind ? @"1":@"0";
        _updateParameters[@"integralRemind"] = self.sateModel.integralRemind ? @"1":@"0";
        _updateParameters[@"location"] = self.sateModel.location ? @"1":@"0";
        _updateParameters[@"orderRemind"] = self.sateModel.orderRemind ? @"1":@"0";
        _updateParameters[@"systemRemind"] = self.sateModel.systemRemind ? @"1":@"0";
    }
    return _updateParameters;
}

@end
